<?php
define("G", "src/");


function create_img_button($label, $img, $font_name, $font_size, $color, $width, $height, $sizew, $sizeh, $position) { 
	$button = new GtkButton(); // create a standard button
	$button->set_size_request($sizew, $sizeh);
	//$button->connect('clicked', array($this , $callback));
	if($position == "horizontal"){$button_box = new GtkHBox();}else{$button_box = new GtkVBox();}
	$button->add($button_box);
	$imag = new GtkImage();
	$imge=GdkPixbuf::new_from_file($img)->scale_simple($width, $height, Gdk::INTERP_HYPER);
	$imag->set_from_pixbuf($imge);
	$button_box->pack_start($imag, 0, 0);
	$button_box->pack_start(new GtkLabel(), 0, 0); // THIS IS A FUCKING SPACER
	$btnLabel = new GtkLabel($label);
	$btnLabel->modify_font(new PangoFontDescription("{$font_name} {$font_size}"));
	$btnLabel->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse($color));
	$button_box->pack_start($btnLabel, 0, 0);

	return $button;
}



//Read the client.ini file
$ini = file_get_contents("client.ini");
$iniArr = explode("\n" , $ini);
$connArr = explode(":" , $iniArr[0]);
//Grab server values from the ini file.
$ip = $connArr['0']; $port = $connArr['1']; $current_version = $iniArr[1]; $email_offlines = $iniArr[2];

$updated = 0;
//Update software if applicable. Send update request to the server.
if($fp = fsockopen($ip, $port)){
	$req = "UPDATE|CLN|" . $current_version . "\n";
	unset($buffer);
	$buffer = "";
	fwrite($fp, $req);
	while(!feof($fp)){
		$buffer .= fgets($fp, 4096);
	}
	fclose($fp);
	$buffArr = explode("<><||><>" , $buffer);
	if($buffArr[1] == "OK"){
		$f_cl = fopen(G . "client.php", "w");
		$to_put = $buffArr[2];
		fwrite($f_cl , $to_put);
		fclose($f_cl);
		
		//Now rebuild the client.ini file with the correct version value
		$fp_ini = fopen("client.ini", "w");
		$new_ini = $ip. ":" . $port . "\n" . $buffArr[0] . "\n" . $email_offlines;
		fwrite($fp_ini , $new_ini);
		fclose($fp_ini);
		$newver =$buffArr[0];
		
		$updated = 1;
		
	}
	unset($buffer);
	unset($fp);
	unset($fp_ini);
}

usleep(80000);

//Update ressources (images) if applicable. Send update request to the server.
if($fp = fsockopen($ip, $port)){
	$req = "UPDATE|RES|" . $current_version . "\n";
	unset($buffer);
	$buffer = "";
	fwrite($fp, $req);
	while(!feof($fp)){
		$buffer .= fgets($fp, 4096);
	}
	fclose($fp);
	$buffArr = explode("<><||><>" , $buffer);
	if($buffArr[1] == "OK"){
		$filesArr = explode("<<||<><>||||69||||<><>||>>" , $buffArr[2]);
		foreach($filesArr as $res_file){
			$res_fileArr = explode("<<||FILENAME_DELIMITER||>>" , $res_file);
			$filename = $res_fileArr[0];
			$filelogic = $res_fileArr[1];
			
			//echo "grabbing: " . $filename . "...\n";

			$f_res = fopen(G . "images/" . $filename, "w");
			fwrite($f_res , $filelogic);
			fclose($f_res);
			
			unset($f_res);
			
		}
		
		
		//Now rebuild the client.ini file with the correct version value
		$newver =$buffArr[0];
		$fp_ini = fopen("client.ini", "w");
		$new_ini = $ip. ":" . $port . "\n" . $buffArr[0] . "\n" . $email_offlines;
		fwrite($fp_ini , $new_ini);
		fclose($fp_ini);
		$updated = 1;
	
	}
	unset($buffer);
	unset($fp);
	unset($fp_ini);
}

if($updated == 1){
	//Maybe OK to have a modal dialog to tell them to hold while updating...
		
	$err = new GtkWindow();
	$err->set_resizable(false);
	$err->set_modal(true);
	$err->set_title("Application UPDATED SUCCESFULLY !");
	$err->set_position(Gtk::WIN_POS_CENTER);
	$err->set_icon_from_file(G . "images/chat.png");
	
	$tbl = new GtkTable();
	$lbl = new GtkLabel("The A-MESSenger client has been updated succesfully!");
	$lbl->modify_font(new PangoFontDescription("tahoma 9"));
	$lbl->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse("blue"));
	
	$lblA = new GtkLabel("Please click OK to close the application and than restart it again to enjoy the new features.");
	$lblA->modify_font(new PangoFontDescription("tahoma 9"));
	$lblA->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse("blue"));
	
	$btn = create_img_button("OK", G. "images/status.png", "tahoma", "9", "blue", "16", "16", "90", "28", "horizontal");
	
	$tbl->attach($lbl,0,1,0,1, Gtk::FILL, Gtk::FILL);
	$tbl->attach($lblA,0,1,1,2, Gtk::FILL, Gtk::FILL);
	$tbl->attach($btn,1,2,0,1, Gtk::SHRINK, Gtk::SHRINK);
	
	$btn->connect_simple('clicked' , array('Gtk', 'main_quit'));
	
	$err->add($tbl);
	$err->show_all();
}




include G ."client.php";
?>